home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Other / power / getdate.y < prev    next >
Encoding:
Text File  |  1992-05-01  |  12.2 KB  |  544 lines

  1. %token ID MONTH DAY MERIDIAN NUMBER UNIT MUNIT SUNIT ZONE DAYZONE AGO
  2. %{
  3.     /*     Steven M. Bellovin (unc!smb)            */
  4.     /*    Dept. of Computer Science            */
  5.     /*    University of North Carolina at Chapel Hill    */
  6.     /*    @(#)getdate.y    2.13    9/16/86 */
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/timeb.h>
  11. #include <ctype.h>
  12. #include <time.h>
  13.  
  14. #define daysec (24L*60L*60L)
  15.  
  16.     static int timeflag, zoneflag, dateflag, dayflag, relflag;
  17.     static time_t relsec, relmonth;
  18.     static int hh, mm, ss, merid, daylight;
  19.     static int dayord, dayreq;
  20.     static int month, day, year;
  21.     static int ourzone;
  22.  
  23. #define AM 1
  24. #define PM 2
  25. #define DAYLIGHT 1
  26. #define STANDARD 2
  27. #define MAYBE    3
  28. %}
  29.  
  30. %%
  31. timedate:         /* empty */
  32.     | timedate item;
  33.  
  34. item:    tspec =
  35.         {timeflag++;}
  36.     | zone =
  37.         {zoneflag++;}
  38.     | dtspec =
  39.         {dateflag++;}
  40.     | dyspec =
  41.         {dayflag++;}
  42.     | rspec =
  43.         {relflag++;}
  44.     | nspec;
  45.  
  46. nspec:    NUMBER =
  47.         {if (timeflag && dateflag && !relflag) year = $1;
  48.         else {timeflag++;hh = $1/100;mm = $1%100;ss = 0;merid = 24;}};
  49.  
  50. tspec:    NUMBER MERIDIAN =
  51.         {hh = $1; mm = 0; ss = 0; merid = $2;}
  52.     | NUMBER ':' NUMBER =
  53.         {hh = $1; mm = $3; merid = 24;}
  54.     | NUMBER ':' NUMBER MERIDIAN =
  55.         {hh = $1; mm = $3; merid = $4;}
  56.     | NUMBER ':' NUMBER NUMBER =
  57.         {hh = $1; mm = $3; merid = 24;
  58.         daylight = STANDARD; ourzone = $4%100 + 60*$4/100;}
  59.     | NUMBER ':' NUMBER ':' NUMBER =
  60.         {hh = $1; mm = $3; ss = $5; merid = 24;}
  61.     | NUMBER ':' NUMBER ':' NUMBER MERIDIAN =
  62.         {hh = $1; mm = $3; ss = $5; merid = $6;}
  63.     | NUMBER ':' NUMBER ':' NUMBER NUMBER =
  64.         {hh = $1; mm = $3; ss = $5; merid = 24;
  65.         daylight = STANDARD; ourzone = $6%100 + 60*$6/100;};
  66.  
  67. zone:    ZONE =
  68.         {ourzone = $1; daylight = STANDARD;}
  69.     | DAYZONE =
  70.         {ourzone = $1; daylight = DAYLIGHT;};
  71.  
  72. dyspec:    DAY =
  73.         {dayord = 1; dayreq = $1;}
  74.     | DAY ',' =
  75.         {dayord = 1; dayreq = $1;}
  76.     | NUMBER DAY =
  77.         {dayord = $1; dayreq = $2;};
  78.  
  79. dtspec:    NUMBER '/' NUMBER =
  80.         {month = $1; day = $3;}
  81.     | NUMBER '/' NUMBER '/' NUMBER =
  82.         {month = $1; day = $3; year = $5;}
  83.     | MONTH NUMBER =
  84.         {month = $1; day = $2;}
  85.     | MONTH NUMBER ',' NUMBER =
  86.         {month = $1; day = $2; year = $4;}
  87.     | NUMBER MONTH =
  88.         {month = $2; day = $1;}
  89.     | NUMBER MONTH NUMBER =
  90.         {month = $2; day = $1; year = $3;};
  91.  
  92.  
  93. rspec:    NUMBER UNIT =
  94.         {relsec +=  60L * $1 * $2;}
  95.     | NUMBER MUNIT =
  96.         {relmonth += $1 * $2;}
  97.     | NUMBER SUNIT =
  98.         {relsec += $1;}
  99.     | UNIT =
  100.         {relsec +=  60L * $1;}
  101.     | MUNIT =
  102.         {relmonth += $1;}
  103.     | SUNIT =
  104.         {relsec++;}
  105.     | rspec AGO =
  106.         {relsec = -relsec; relmonth = -relmonth;};
  107. %%
  108.  
  109. static int mdays[12] =
  110.     {31, 0, 31,  30, 31, 30,  31, 31, 30,  31, 30, 31};
  111. #define epoch 1970
  112.  
  113. extern struct tm *localtime();
  114.  
  115. static time_t
  116. dateconv(mm, dd, yy, h, m, s, mer, zone, dayflag)
  117. int mm, dd, yy, h, m, s, mer, zone, dayflag;
  118. {
  119.     time_t tod, jdate;
  120.     register int i;
  121.     time_t timeconv();
  122.  
  123.     if (yy < 0) yy = -yy;
  124.     if (yy < 100) yy += 1900;
  125.     mdays[1] = 28 + (yy%4 == 0 && (yy%100 != 0 || yy%400 == 0));
  126.     if (yy < epoch || yy > 1999 || mm < 1 || mm > 12 ||
  127.         dd < 1 || dd > mdays[--mm]) return (-1);
  128.     jdate = dd-1;
  129.         for (i=0; i<mm; i++) jdate += mdays[i];
  130.     for (i = epoch; i < yy; i++) jdate += 365 + (i%4 == 0);
  131.     jdate *= daysec;
  132.     jdate += zone * 60L;
  133.     if ((tod = timeconv(h, m, s, mer)) < 0) return (-1);
  134.     jdate += tod;
  135.     if (dayflag==DAYLIGHT || (dayflag==MAYBE&&localtime(&jdate)->tm_isdst))
  136.         jdate += -1*60*60;
  137.     return (jdate);
  138. }
  139.  
  140. static time_t
  141. dayconv(ord, day, now)
  142. int ord, day; time_t now;
  143. {
  144.     register struct tm *loctime;
  145.     time_t tod;
  146.     time_t daylcorr();
  147.  
  148.     tod = now;
  149.     loctime = localtime(&tod);
  150.     tod += daysec * ((day - loctime->tm_wday + 7) % 7);
  151.     tod += 7*daysec*(ord<=0?ord:ord-1);
  152.     return daylcorr(tod, now);
  153. }
  154.  
  155. static time_t
  156. timeconv(hh, mm, ss, mer)
  157. register int hh, mm, ss, mer;
  158. {
  159.     if (mm < 0 || mm > 59 || ss < 0 || ss > 59) return (-1);
  160.     switch (mer) {
  161.         case AM: if (hh < 1 || hh > 12) return(-1);
  162.              return (60L * ((hh%12)*60L + mm)+ss);
  163.         case PM: if (hh < 1 || hh > 12) return(-1);
  164.              return (60L * ((hh%12 +12)*60L + mm)+ss);
  165.         case 24: if (hh < 0 || hh > 23) return (-1);
  166.              return (60L * (hh*60L + mm)+ss);
  167.         default: return (-1);
  168.     }
  169. }
  170.  
  171. static time_t
  172. monthadd(sdate, relmonth)
  173. time_t sdate, relmonth;
  174. {
  175.     struct tm *ltime;
  176.     time_t dateconv();
  177.     time_t daylcorr();
  178.     int mm, yy;
  179.  
  180.     if (relmonth == 0) return 0;
  181.     ltime = localtime(&sdate);
  182.     mm = 12*ltime->tm_year + ltime->tm_mon + relmonth;
  183.     yy = mm/12;
  184.     mm = mm%12 + 1;
  185.     return daylcorr(dateconv(mm, ltime->tm_mday, yy, ltime->tm_hour,
  186.         ltime->tm_min, ltime->tm_sec, 24, ourzone, MAYBE), sdate);
  187. }
  188.  
  189. static time_t
  190. daylcorr(future, now)
  191. time_t future, now;
  192. {
  193.     int fdayl, nowdayl;
  194.  
  195.     nowdayl = (localtime(&now)->tm_hour+1) % 24;
  196.     fdayl = (localtime(&future)->tm_hour+1) % 24;
  197.     return (future-now) + 60L*60L*(nowdayl-fdayl);
  198. }
  199.  
  200. static char *lptr;
  201.  
  202. yylex()
  203. {
  204.     extern int yylval;
  205.     int sign;
  206.     register char c;
  207.     register char *p;
  208.     char idbuf[20];
  209.     int pcnt;
  210.  
  211.     for (;;) {
  212.         while (isspace(*lptr)) lptr++;
  213.  
  214.         if (isdigit(c = *lptr) || c == '-' || c == '+') {
  215.             if (c== '-' || c == '+') {
  216.                 if (c=='-') sign = -1;
  217.                 else sign = 1;
  218.                 if (!isdigit(*++lptr)) {
  219.                     /* yylval = sign; return (NUMBER); */
  220.                     return yylex();    /* skip the '-' sign */
  221.                 }
  222.             } else sign = 1;
  223.             yylval = 0;
  224.             while (isdigit(c = *lptr++)) yylval = 10*yylval + c - '0';
  225.             yylval *= sign;
  226.             lptr--;
  227.             return (NUMBER);
  228.  
  229.         } else if (isalpha(c)) {
  230.             p = idbuf;
  231.             while (isalpha(c = *lptr++) || c=='.')
  232.                 if (p < &idbuf[sizeof(idbuf)-1])
  233.                     *p++ = c;
  234.             *p = '\0';
  235.             lptr--;
  236.             return (lookup(idbuf));
  237.         }
  238.  
  239.         else if (c == '(') {
  240.             pcnt = 0;
  241.             do {
  242.                 c = *lptr++;
  243.                 if (c == '\0') return(c);
  244.                 else if (c == '(') pcnt++;
  245.                 else if (c == ')') pcnt--;
  246.             } while (pcnt > 0);
  247.         }
  248.  
  249.         else return (*lptr++);
  250.     }
  251. }
  252.  
  253. struct table {
  254.     char *name;
  255.     int type, value;
  256. };
  257.  
  258. static struct table mdtab[] = {
  259.     {"January", MONTH, 1},
  260.     {"February", MONTH, 2},
  261.     {"March", MONTH, 3},
  262.     {"April", MONTH, 4},
  263.     {"May", MONTH, 5},
  264.     {"June", MONTH, 6},
  265.     {"July", MONTH, 7},
  266.     {"August", MONTH, 8},
  267.     {"September", MONTH, 9},
  268.     {"Sept", MONTH, 9},
  269.     {"October", MONTH, 10},
  270.     {"November", MONTH, 11},
  271.     {"December", MONTH, 12},
  272.  
  273.     {"Sunday", DAY, 0},
  274.     {"Monday", DAY, 1},
  275.     {"Tuesday", DAY, 2},
  276.     {"Tues", DAY, 2},
  277.     {"Wednesday", DAY, 3},
  278.     {"Wednes", DAY, 3},
  279.     {"Thursday", DAY, 4},
  280.     {"Thur", DAY, 4},
  281.     {"Thurs", DAY, 4},
  282.     {"Friday", DAY, 5},
  283.     {"Saturday", DAY, 6},
  284.     {0, 0, 0}};
  285.  
  286. #define HRS *60
  287. #define HALFHR 30
  288. static struct table mztab[] = {
  289.     {"a.m.", MERIDIAN, AM},
  290.     {"am", MERIDIAN, AM},
  291.     {"p.m.", MERIDIAN, PM},
  292.     {"pm", MERIDIAN, PM},
  293.     {"nst", ZONE, 3 HRS + HALFHR},        /* Newfoundland */
  294.     {"n.s.t.", ZONE, 3 HRS + HALFHR},
  295.     {"ast", ZONE, 4 HRS},        /* Atlantic */
  296.     {"a.s.t.", ZONE, 4 HRS},
  297.     {"adt", DAYZONE, 4 HRS},
  298.     {"a.d.t.", DAYZONE, 4 HRS},
  299.     {"est", ZONE, 5 HRS},        /* Eastern */
  300.     {"e.s.t.", ZONE, 5 HRS},
  301.     {"edt", DAYZONE, 5 HRS},
  302.     {"e.d.t.", DAYZONE, 5 HRS},
  303.     {"cst", ZONE, 6 HRS},        /* Central */
  304.     {"c.s.t.", ZONE, 6 HRS},
  305.     {"cdt", DAYZONE, 6 HRS},
  306.     {"c.d.t.", DAYZONE, 6 HRS},
  307.     {"mst", ZONE, 7 HRS},        /* Mountain */
  308.     {"m.s.t.", ZONE, 7 HRS},
  309.     {"mdt", DAYZONE, 7 HRS},
  310.     {"m.d.t.", DAYZONE, 7 HRS},
  311.     {"pst", ZONE, 8 HRS},        /* Pacific */
  312.     {"p.s.t.", ZONE, 8 HRS},
  313.     {"pdt", DAYZONE, 8 HRS},
  314.     {"p.d.t.", DAYZONE, 8 HRS},
  315.     {"yst", ZONE, 9 HRS},        /* Yukon */
  316.     {"y.s.t.", ZONE, 9 HRS},
  317.     {"ydt", DAYZONE, 9 HRS},
  318.     {"y.d.t.", DAYZONE, 9 HRS},
  319.     {"hst", ZONE, 10 HRS},        /* Hawaii */
  320.     {"h.s.t.", ZONE, 10 HRS},
  321.     {"hdt", DAYZONE, 10 HRS},
  322.     {"h.d.t.", DAYZONE, 10 HRS},
  323.  
  324.     {"gmt", ZONE, 0 HRS},
  325.     {"g.m.t.", ZONE, 0 HRS},
  326.     {"bst", DAYZONE, 0 HRS},        /* British Summer Time */
  327.     {"b.s.t.", DAYZONE, 0 HRS},
  328.     {"eet", ZONE, 0 HRS},        /* European Eastern Time */
  329.     {"e.e.t.", ZONE, 0 HRS},
  330.     {"eest", DAYZONE, 0 HRS},    /* European Eastern Summer Time */
  331.     {"e.e.s.t.", DAYZONE, 0 HRS},
  332.     {"met", ZONE, -1 HRS},        /* Middle European Time */
  333.     {"m.e.t.", ZONE, -1 HRS},
  334.     {"mest", DAYZONE, -1 HRS},    /* Middle European Summer Time */
  335.     {"m.e.s.t.", DAYZONE, -1 HRS},
  336.     {"wet", ZONE, -2 HRS },        /* Western European Time */
  337.     {"w.e.t.", ZONE, -2 HRS },
  338.     {"west", DAYZONE, -2 HRS},    /* Western European Summer Time */
  339.     {"w.e.s.t.", DAYZONE, -2 HRS},
  340.  
  341.     {"jst", ZONE, -9 HRS},        /* Japan Standard Time */
  342.     {"j.s.t.", ZONE, -9 HRS},    /* Japan Standard Time */
  343.                     /* No daylight savings time */
  344.  
  345.     {"aest", ZONE, -10 HRS},    /* Australian Eastern Time */
  346.     {"a.e.s.t.", ZONE, -10 HRS},
  347.     {"aesst", DAYZONE, -10 HRS},    /* Australian Eastern Summer Time */
  348.     {"a.e.s.s.t.", DAYZONE, -10 HRS},
  349.     {"acst", ZONE, -(9 HRS + HALFHR)},    /* Australian Central Time */
  350.     {"a.c.s.t.", ZONE, -(9 HRS + HALFHR)},
  351.     {"acsst", DAYZONE, -(9 HRS + HALFHR)},    /* Australian Central Summer */
  352.     {"a.c.s.s.t.", DAYZONE, -(9 HRS + HALFHR)},
  353.     {"awst", ZONE, -8 HRS},        /* Australian Western Time */
  354.     {"a.w.s.t.", ZONE, -8 HRS},    /* (no daylight time there, I'm told */
  355.     {0, 0, 0}};
  356.  
  357. static struct table unittb[] = {
  358.     {"year", MUNIT, 12},
  359.     {"month", MUNIT, 1},
  360.     {"fortnight", UNIT, 14*24*60},
  361.     {"week", UNIT, 7*24*60},
  362.     {"day", UNIT, 1*24*60},
  363.     {"hour", UNIT, 60},
  364.     {"minute", UNIT, 1},
  365.     {"min", UNIT, 1},
  366.     {"second", SUNIT, 1},
  367.     {"sec", SUNIT, 1},
  368.     {0, 0, 0}};
  369.  
  370. static struct table othertb[] = {
  371.     {"tomorrow", UNIT, 1*24*60},
  372.     {"yesterday", UNIT, -1*24*60},
  373.     {"today", UNIT, 0},
  374.     {"now", UNIT, 0},
  375.     {"last", NUMBER, -1},
  376.     {"this", UNIT, 0},
  377.     {"next", NUMBER, 2},
  378.     {"first", NUMBER, 1},
  379.     /* {"second", NUMBER, 2}, */
  380.     {"third", NUMBER, 3},
  381.     {"fourth", NUMBER, 4},
  382.     {"fifth", NUMBER, 5},
  383.     {"sixth", NUMBER, 6},
  384.     {"seventh", NUMBER, 7},
  385.     {"eigth", NUMBER, 8},
  386.     {"ninth", NUMBER, 9},
  387.     {"tenth", NUMBER, 10},
  388.     {"eleventh", NUMBER, 11},
  389.     {"twelfth", NUMBER, 12},
  390.     {"ago", AGO, 1},
  391.     {0, 0, 0}};
  392.  
  393. static struct table milzone[] = {
  394.     {"a", ZONE, 1 HRS},
  395.     {"b", ZONE, 2 HRS},
  396.     {"c", ZONE, 3 HRS},
  397.     {"d", ZONE, 4 HRS},
  398.     {"e", ZONE, 5 HRS},
  399.     {"f", ZONE, 6 HRS},
  400.     {"g", ZONE, 7 HRS},
  401.     {"h", ZONE, 8 HRS},
  402.     {"i", ZONE, 9 HRS},
  403.     {"k", ZONE, 10 HRS},
  404.     {"l", ZONE, 11 HRS},
  405.     {"m", ZONE, 12 HRS},
  406.     {"n", ZONE, -1 HRS},
  407.     {"o", ZONE, -2 HRS},
  408.     {"p", ZONE, -3 HRS},
  409.     {"q", ZONE, -4 HRS},
  410.     {"r", ZONE, -5 HRS},
  411.     {"s", ZONE, -6 HRS},
  412.     {"t", ZONE, -7 HRS},
  413.     {"u", ZONE, -8 HRS},
  414.     {"v", ZONE, -9 HRS},
  415.     {"w", ZONE, -10 HRS},
  416.     {"x", ZONE, -11 HRS},
  417.     {"y", ZONE, -12 HRS},
  418.     {"z", ZONE, 0 HRS},
  419.     {0, 0, 0}};
  420.  
  421. static
  422. lookup(id)
  423. char *id;
  424. {
  425. #define gotit (yylval=i->value,  i->type)
  426. #define getid for(j=idvar, k=id; *j++ = *k++; )
  427.  
  428.     char idvar[20];
  429.     register char *j, *k;
  430.     register struct table *i;
  431.     int abbrev;
  432.  
  433.     getid;
  434.     if (strlen(idvar) == 3) abbrev = 1;
  435.     else if (strlen(idvar) == 4 && idvar[3] == '.') {
  436.         abbrev = 1;
  437.         idvar[3] = '\0';
  438.     }
  439.     else abbrev = 0;
  440.  
  441.     if (islower(*idvar)) *idvar = toupper(*idvar);
  442.  
  443.     for (i = mdtab; i->name; i++) {
  444.         k = idvar;
  445.         for (j = i->name; *j++ == *k++;) {
  446.             if (abbrev && j==i->name+3) return gotit;
  447.             if (j[-1] == 0) return gotit;
  448.         }
  449.     }
  450.  
  451.     getid;
  452.     for (i = mztab; i->name; i++)
  453.         if (strcmp(i->name, idvar) == 0) return gotit;
  454.  
  455.     for (j = idvar; *j; j++)
  456.         if (isupper(*j)) *j = tolower(*j);
  457.     for (i=mztab; i->name; i++)
  458.         if (strcmp(i->name, idvar) == 0) return gotit;
  459.  
  460.     getid;
  461.     for (i=unittb; i->name; i++)
  462.         if (strcmp(i->name, idvar) == 0) return gotit;
  463.  
  464.     if (idvar[strlen(idvar)-1] == 's')
  465.         idvar[strlen(idvar)-1] = '\0';
  466.     for (i=unittb; i->name; i++)
  467.         if (strcmp(i->name, idvar) == 0) return gotit;
  468.  
  469.     getid;
  470.     for (i = othertb; i->name; i++)
  471.         if (strcmp(i->name, idvar) == 0) return gotit;
  472.  
  473.     getid;
  474.     if (strlen(idvar) == 1 && isalpha(*idvar)) {
  475.         if (isupper(*idvar)) *idvar = tolower(*idvar);
  476.         for (i = milzone; i->name; i++)
  477.             if (strcmp(i->name, idvar) == 0) return gotit;
  478.     }
  479.  
  480.     return(ID);
  481. }
  482.  
  483. time_t
  484. getdate(p, now)
  485. char *p;
  486. struct timeb *now;
  487. {
  488. #define mcheck(f)    if (f>1) err++
  489.     time_t monthadd();
  490.     int err;
  491.     struct tm *lt;
  492.     struct timeb ftz;
  493.  
  494.     time_t sdate, tod;
  495.  
  496.     lptr = p;
  497.     if (now == ((struct timeb *) NULL)) {
  498.         now = &ftz;
  499.         ftime(&ftz);
  500.     }
  501.     lt = localtime(&now->time);
  502.     year = lt->tm_year;
  503.     month = lt->tm_mon+1;
  504.     day = lt->tm_mday;
  505.     relsec = 0; relmonth = 0;
  506.     timeflag=zoneflag=dateflag=dayflag=relflag=0;
  507.     ourzone = now->timezone;
  508.     daylight = MAYBE;
  509.     hh = mm = ss = 0;
  510.     merid = 24;
  511.  
  512.     if (err = yyparse()) return (-1);
  513.  
  514.     mcheck(timeflag);
  515.     mcheck(zoneflag);
  516.     mcheck(dateflag);
  517.     mcheck(dayflag);
  518.  
  519.     if (err) return (-1);
  520.     if (dateflag || timeflag || dayflag) {
  521.         sdate = dateconv(month,day,year,hh,mm,ss,merid,ourzone,daylight);
  522.         if (sdate < 0) return -1;
  523.     }
  524.     else {
  525.         sdate = now->time;
  526.         if (relflag == 0)
  527.             sdate -= (lt->tm_sec + lt->tm_min*60 +
  528.                 lt->tm_hour*(60L*60L));
  529.     }
  530.  
  531.     sdate += relsec;
  532.     sdate += monthadd(sdate, relmonth);
  533.  
  534.     if (dayflag && !dateflag) {
  535.         tod = dayconv(dayord, dayreq, sdate);
  536.         sdate += tod;
  537.     }
  538.  
  539.     return sdate;
  540. }
  541.  
  542. yyerror(s) char *s;
  543. {}
  544.